package org.zjsru.product.web;

import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zjsru.product.pojo.Order;
import org.zjsru.product.pojo.User;
import org.zjsru.product.service.ProductService;
import org.zjsru.product.service.ProductServiceFeign;

@RefreshScope
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService  productService;


    @Value("${custom.welcomeMessage}")
    private String welcomeMessage;

    @GetMapping("/welcome")
    public String getWelcomeMessage() {
        return welcomeMessage;
        /*return "welcomeMessage";*/
    }
    @Autowired
    ProductServiceFeign productServiceFeign ;
    @GetMapping("/{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) throws NacosException {
        // 根据id查询订单并返回
        Order order = productService.queryOrderById(orderId);
        /*ServiceInstance instance = serviceDiscovery.getInstances("user-service").get(0);
        String url = instance.getHost() + ":" + instance.getPort() ;
        User user = restTemplate.getForObject("http://" + url + "/user/" + order.getUserId(), User.class);
        //User user = restTemplate.getForObject("http://user-service/user/" + order.getUserId(), User.class);*/
        User user = productServiceFeign.queryUserById(order.getUserId());
        order.setUser(user);
        return order;
    }
}
